package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.warning.domain.entity.Contacts;
import com.ruoyi.warning.domain.entity.Dispose;
import com.ruoyi.warning.domain.entity.Event;
import com.ruoyi.warning.mapper.DisposeMapper;
import com.ruoyi.warning.service.IContactsService;
import com.ruoyi.warning.service.IDisposeService;
import com.ruoyi.warning.service.IEventService;
import com.ruoyi.warning.service.WarningSendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 * 告警事件处理表 服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-05-16
 */
@Service
public class DisposeServiceImpl extends ServiceImpl<DisposeMapper, Dispose> implements IDisposeService {
    @Resource
    private WarningSendService warningSendService;
    @Resource
    private IContactsService contactsService;
    @Resource
    private IEventService eventService;
    @Override
    public Object dispose(Dispose dispose) {
        if(dispose.getDisposeStatus().equals("处理中")) {
            if (ObjectUtils.isNotEmpty(dispose.getDisposeContact())) {
                Contacts contacts = contactsService.getById(dispose.getDisposeContact());
                Event event = eventService.getById(dispose.getEventId());
                ExecutorService executorService = Executors.newFixedThreadPool(5);
                executorService.execute(() -> {
                    try {
                        dispose.setAcceptContact(warningSendService.disposeSms(contacts,event));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    } finally {
                        // 关闭线程池
                        executorService.shutdown();
                    }
                });
            }
        }
        this.saveOrUpdate(dispose);
        return "发送成功";
    }
}
